package cn.ulc.socket.admin.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.common.util.Result;
import cn.ulc.socket.admin.model.dto.goods.GoodsListDTO;
import cn.ulc.socket.admin.model.param.goods.GoodsListParam;
import cn.ulc.socket.admin.model.param.goods.GoodsStockWarningParam;
import cn.ulc.socket.admin.model.param.goods.GoodsSaveParam;
import cn.ulc.socket.admin.service.goods.GoodsServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/goods")
public class GoodsCtrl {
    private static final Log log = LogFactory.get();

    @Autowired
    GoodsServ goodsServ;

    @GetMapping("/water/list")
    public Result findWaterListPage(String name, Long goodsTypeId, PaginationParam pager) {
        GoodsListDTO dto = goodsServ.findWaterListPage(name,goodsTypeId,pager);
        return Result.success(dto);
    }
    @GetMapping("")
    public Result findListPage(String name, Long goodsTypeId, PaginationParam pager) {
        GoodsListDTO dto = goodsServ.findListPage(name,goodsTypeId,pager);
        return Result.success(dto);
    }

    @GetMapping("/{id}")
    public Result findDetail(@PathVariable Long id) {
        return Result.success(goodsServ.findDetail(id));
    }
    @PostMapping
    public Result save(@Validated @RequestBody GoodsSaveParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存商品详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(goodsServ.save(param));
    }
    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Long id, @RequestBody GoodsSaveParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存商品详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        param.setId(id);
        return Result.success(goodsServ.save(param));
    }
    @DeleteMapping("/{id}")
    public Result deleted(@PathVariable("id") Long id){
        return Result.success(goodsServ.deleted(id));
    }



    //商品上下架
    @PutMapping("/changeStatus/{id}")
    public Result changeStatus(@PathVariable("id") Long id,AvailableStatus status){
        return Result.success(goodsServ.changeStatus(id,status));
    }
}
